package mx.ipn.presentacion.nomina;
import mx.ipn.presentacion.ConexionConNegocios;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QTableWidgetItem;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QDialog;
import mx.ipn.presentacion.nomina.ui.*;
import mx.ipn.to.*;
public class CreaRango extends QDialog{
	Ui_Rango crear;
	RangoTO []arrrango;
	RangoTO rango;
	int id_rango;
	double inicio;
	double fin;
	double porcentaje;
	double valorfijo;
	QTableWidgetItem newItem=null;
	public CreaRango(){
			crear = new Ui_Rango();
			crear.setupUi(this);
			//rep.tableWidget.setRowCount(1);
			//rep.tableWidget.itemClicked.connect(this,"selectFactura()");
			//rep.pushButton_2.clicked.connect(this, "buscar()");
			//rep.radioButton.clicked.connect(this,"mes()");
			//rep.radioButton_2.clicked.connect(this,"dia()");
			//crear.pushButton_2.clicked.connect(this, "eliminar()");
			crear.pushButton.clicked.connect(this, "accept()");
            crear.pushButton_3.clicked.connect(this, "reject()");
			llenatabla();
			this.show();
	}
	public CreaRango(QDialog parent){
			
	}
	public void limpia(){
		crear.tableWidget.clearContents();
	}
	/*public void eliminar(){
		short r1= (short)-1;
		if(tableper==0){
		r1 =((Short)ConexionConNegocios.invocaServicio("eliminaDetalleFactura",idf,Short.class)).shortValue();
	    }
	    if(tableper==1){
	    r1 =((Short)ConexionConNegocios.invocaServicio("eliminaDetalleFactura",idf,Short.class)).shortValue();
	    }
		if(r==1){
			 QMessageBox.information(null,"informacion","eliminada");
			 limpia();
	         }
		else{
			 QMessageBox.critical(null, "Error",
						"No se borro nungun dato.",
						QMessageBox.StandardButton.Ok,
						QMessageBox.StandardButton.Ok);}
	}*/
	public void nuevorango(){
		
	}
	public void llenatabla(){
		int j=0;
		arrrango=(RangoTO[])ConexionConNegocios.invocaServicio("selectRango",RangoTO[].class);
		if(arrrango!=null){
			crear.tableWidget.clearContents();
			for (RangoTO f : arrrango){
				newItem = new QTableWidgetItem(String.valueOf(arrrango[j].getIdRango()));
	    	    crear.tableWidget.setItem(j,0,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrrango[j].getInicio()));
	    	    crear.tableWidget.setItem(j,1,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(arrrango[j].getFin()));
	    	    crear.tableWidget.setItem(j,2,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(arrrango[j].getPorcentajeSueldo()));
	    	    crear.tableWidget.setItem(j,3,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(arrrango[j].getValorFijo()));
	    	    crear.tableWidget.setItem(j,4,newItem);
	    	    j++;
			}
			crear.tableWidget.insertRow(crear.tableWidget.rowCount());
	    }
	}
	public  static boolean isDouble(String cadena){
		Double num= null;
		try {
			num= new Double(cadena);
			return true;
		} catch (Exception nfe){
			return false;
		}
	}
	public boolean valida(){
		boolean valido=false;
		int fila = crear.tableWidget.rowCount()-1;
		newItem = crear.tableWidget.item(fila,1);
		if(isDouble(newItem.text())){
		   newItem = crear.tableWidget.item(fila,2);
		   if(isDouble(newItem.text())){
		      newItem = crear.tableWidget.item(fila,3);
		      if(isDouble(newItem.text())){
		         newItem = crear.tableWidget.item(fila,4);
		         if(isDouble(newItem.text())){
		        	 valido=true;
		           }
		         }
		      }
		   }
		return valido;
	}
	public void accept(){
	   Double num= null;
	   int fila = crear.tableWidget.rowCount();
	   newItem = crear.tableWidget.item(fila-1,1);
	   num= new Double(newItem.text());
	   inicio = num.doubleValue();
	   newItem = crear.tableWidget.item(fila-1,2);
	   num= new Double(newItem.text());
	   fin = num.doubleValue();
	   newItem = crear.tableWidget.item(fila-1,3);
	   num= new Double(newItem.text());
	   porcentaje = num.doubleValue();
	   newItem = crear.tableWidget.item(fila-1,4);
	   num= new Double(newItem.text());
	   valorfijo = num.doubleValue();
	   rango = new RangoTO(0,inicio,fin,porcentaje,valorfijo);
			  if(valida()&&rango!=null){
			        boolean r=((Boolean)ConexionConNegocios.invocaServicio("addRango",rango,Boolean.class)).booleanValue();
			        if(r)
			           QMessageBox.information(null,"informacion","guardada");
			        crear.tableWidget.insertRow(crear.tableWidget.rowCount());
		      }
	}
	public void reject(){
		this.dispose();
	}
}
